const Request = require('request')
const Cheerio = require('cheerio')
const fs = require('fs')
const path = require('path')
function Creeper(){
    let targetUrlList = []
    let imageList = [] // 用于存图片地址
    this.setTargetUrlList = function(urlList = []) {
        targetUrlList = urlList
        console.info(`设置目标列表完成`)
    }

    // 爬取url内容并保存进数组
    this.getUrlContent = function() {
        return new Promise((resolve, reject) => {
            getImgUrl(0, function() {
                resolve()
            })
        })

    }
    this.downLoadImg = downLoadImg
    // 下载图片
    function downLoadImg(index = 0) {
        const timestamp = new Date().getTime()
        const readStream = Request({url: imageList[index]})
        saveFile(`pic${timestamp}`, readStream, function() {
            if(++index < imageList.length) {
                downLoadImg(index)
            } else {
                console.info('图片全部存储完成')
            }
        })
    }
    // 请求url拿到图片地址
    function getImgUrl(index = 0, callback = function() {}) {
        console.log(targetUrlList[index])
        Request(targetUrlList[index], function(err, res, body) {
            if(err) {
                console.info(err)
            }
            console.info('开始解析html标签')
            const $ = Cheerio.load(body)
            $('.threadlist_pic').each((idx, ele) => {
                console.log($(ele).attr('bpic'))
                imageList.push($(ele).attr('bpic'))
            })
            console.info('解析结束')
            if(++index < targetUrlList.length) {
                getImgUrl(index, callback)
            } else {
                callback()
            }
        })
    }
    // 保存文件
    function saveFile(fileName = 'demo', readStream, callback = function() {}) {
        // const fileBuffer = Buffer.from(str, 'utf-8') // 字符串转成缓冲区
        const writeStream = fs.createWriteStream(path.join(__dirname,`../assets/${fileName}.jpg`)) // 创建可写流对象
        readStream.pipe(writeStream)
        readStream.on('end', () => {
            console.info('文件写入成功')
            writeStream.end()
            callback()
        })
    }
}

module.exports = Creeper